﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cookie.Library.Extensions
{
    public static class ArrayExtension
    {
        /// <summary>
        /// 从集合中随机选取一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static T GetRandomItem<T>(this List<T> list)
        {
            var count = list.Count;
            if(count == 0) return default(T);
            var index = new Random().Next(count);
            return list[index];
        }

        public static T GetRandomItem<T>(this T[] array)
        {
            var count = array.Length;
            if (count == 0) return default(T);
            var index = new Random().Next(count);
            return array[index];
        }
    }
}
